package cn.rjzjh.tapestry.component.components;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;

import cn.rjzjh.tapestry.component.tools.ComponentsAssist;

/****
 * 对象框
 * 
 * @author 周俊辉
 * 
 */
@Import(stack = "easyuistack")
public class Dialog extends Window {
	@Parameter(required = false, defaultPrefix = BindingConstants.LITERAL)
	private JSONArray toolbar;

	@Parameter(required = false, defaultPrefix = BindingConstants.LITERAL)
	private JSONArray buttons;

	@Override
	protected void writeFieldTag(MarkupWriter writer, final JSONObject spec) {
		packSpec(spec);
		writer.element("div", "id", getClientId());
		javaScriptSupport.require("init").invoke("dialog").with(spec);
	}

	@AfterRender
	final void afterDialog(MarkupWriter writer) {
		if (resources.isBound("toolbar")) {
			writer.element("div", "id", getClientId() + "_toolbar");
			ComponentsAssist.renderButs(writer, toolbar, null, resources);
			writer.end();
		}
		if (resources.isBound("buttons")) {
			writer.element("div", "id", getClientId() + "_buttons");
			ComponentsAssist.renderButs(writer, buttons, null, resources);
			writer.end();
		}
	}

	@Override
	protected void packSpec(JSONObject spec) {
		super.packSpec(spec);
		JSONObject params = spec.getJSONObject("params");
		if (resources.isBound("toolbar"))
			params.put("toolbar", "#" + getClientId() + "_toolbar");

		if (resources.isBound("buttons"))
			params.put("buttons", "#" + getClientId() + "_buttons");

	}

}
